﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using MShop.Core.Domain;
using MShop.Core.Configuration;


namespace MShop.Core.DataAccess
{
    public class ShopSettingsDao : CommonDao<ShopSettings>, IShopSettingsDao
    {

         public ShopSettingsDao(IObjectContextProvider provider)
            : base(provider)
        {
             
        }

         public ShopCfgSettings GetInstance()
         {
             ShopCfgSettings settings = new ShopCfgSettings();
             Type type = settings.GetType();

             System.Collections.Specialized.StringDictionary lazy = Load();

             foreach (string key in lazy.Keys)
             {
                 string name = key;
                 string value = lazy[key];

                 foreach (System.Reflection.PropertyInfo myprop in type.GetProperties())
                 {
                     if (myprop.Name != "Instance")
                     {
                         if (myprop.Name.Equals(name, StringComparison.InvariantCultureIgnoreCase))
                         {
                             if (myprop.PropertyType.IsGenericType)
                             {
                                 if (!string.IsNullOrEmpty(value))
                                 {
                                     Type t = myprop.PropertyType.GetGenericArguments()[0];
                                     myprop.SetValue(settings, Convert.ChangeType(value, t, System.Globalization.CultureInfo.InvariantCulture), null);
                                 }
                             }


                             else if (myprop.PropertyType.IsEnum)
                             { 
                                 if (!string.IsNullOrEmpty(value))
                                 {

                                     myprop.SetValue(settings, Enum.Parse(myprop.PropertyType, value,false), null);
                                 }
                             }

                             else if (myprop.PropertyType.IsValueType && myprop.PropertyType == typeof(Guid))
                             {
                                 if (!string.IsNullOrEmpty(value))
                                 {
                                     myprop.SetValue(settings, Guid.Parse(value), null);
                                 }
                             }
                             else if (myprop.PropertyType.IsValueType && myprop.PropertyType.IsEnum == false)
                             {
                                 if (!string.IsNullOrEmpty(value))
                                 {
                                     myprop.SetValue(settings, Convert.ChangeType(value, myprop.PropertyType, System.Globalization.CultureInfo.InvariantCulture), null);
                                 }
                             }
                             else
                             {
                                 if (!string.IsNullOrEmpty(value))
                                 {
                                     myprop.SetValue(settings, Convert.ChangeType(value, myprop.PropertyType, System.Globalization.CultureInfo.InvariantCulture), null);
                                 }
                             }
                             break;
                         }
                     }
                 }

               
             }
             return settings;
         }



         public void UpdateCfgSettings(ShopCfgSettings instance)
         {
             Type type = instance.GetType();
             System.Collections.Specialized.StringDictionary dicts = new System.Collections.Specialized.StringDictionary();
             foreach (System.Reflection.PropertyInfo myprop in type.GetProperties())
             {
                 if (myprop.Name != "Instance")
                 {
                     object propertyValue = myprop.GetValue(instance, null);
                     string valueAsString = null;
                     if (myprop.PropertyType.IsGenericType && myprop.PropertyType.GetGenericTypeDefinition() == typeof(Nullable<>))
                     {
                         if (propertyValue != null)
                         {
                             valueAsString = propertyValue.ToString();
                         }
                     }
                     else
                     {


                         if (myprop.PropertyType.IsValueType && myprop.PropertyType.IsEnum==false)
                         {
                             if (propertyValue != null)
                             {
                                 valueAsString = propertyValue.ToString();
                             }
                         }
                         else if (myprop.PropertyType.IsValueType && myprop.PropertyType.IsEnum)
                         {
                             if (propertyValue != null)
                             {
                                 valueAsString = Enum.Parse(myprop.PropertyType, propertyValue.ToString()).ToString();
                             }
                         }
                         else
                         {
                             if (propertyValue != null)
                             {
                                 valueAsString = propertyValue.ToString();
                             }
                         }
                     }

                     dicts.Add(myprop.Name, valueAsString);
                 }
             }
             Save(dicts);
             SaveChanges();           
         }


         public void Save(System.Collections.Specialized.StringDictionary dicts)
         {
             IQueryable<ShopSettings> querable = this.GetAll();
             foreach (string key in dicts.Keys)
             {
                  string value = dicts[key];
                  var shopsettings = querable.FirstOrDefault(x => x.SettingName == key);
                  if (shopsettings != null)
                  {
                      shopsettings.SettingValue = value;
                  }
                  else
                  {
                      shopsettings = new ShopSettings() { SettingName = key, SettingValue = value };
                      Save(shopsettings);
                  }
             }
         
         }

         public  System.Collections.Specialized.StringDictionary Load()
        {

            System.Collections.Specialized.StringDictionary dicts = new System.Collections.Specialized.StringDictionary();
            IQueryable<ShopSettings> queryable = this.GetAll();
            foreach (var shopsettings in queryable)
            {
                dicts.Add(shopsettings.SettingName, shopsettings.SettingValue);
            }
            return dicts;  
        }
    }
}